#!/usr/bin/make -f

default: help all
SHELL=/bin/bash
export SHELL
THISDIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

package?=tizen-helper
profile?=common
arch?=$(shell arch)
tmp?=._$(shell date -u +%s).tmp
srcs?=${SELF} \
 $(shell find ${THISDIR}/config -iname "*.in")

profiles?=$(shell grep '\[profile' ${THISDIR}/config/gbs.conf \
 | sed -e 's|\[\(profile\..*\)\]|\1|g' \
 | sort \
)

urls?=$(shell grep 'url=' ${THISDIR}/config/gbs.conf \
 | sed -e 's|url=\(.*\)|\1|g' \
)

SELF?=$(lastword $(MAKEFILE_LIST))
MAKE=make -f ${SELF}
export MAKE

sudo?=sudo

profile_name=$(shell echo ${profile} | cut -d. -f2-)
gbs_dir?=${HOME}/tmp/gbs/tmp-GBS-${profile_name}
repo_dir?=${gbs_dir}/local/repos/${profile_name}/
mount_dir?=${gbs_dir}/local/BUILD-ROOTS/scratch.${arch}.0/proc
gbs?=gbs-safe

help: ${THISDIR}/README.txt
	cat $<
	@echo "Existing profiles:"
	@echo "${profiles}" | tr ' ' '\n'

print:
	@echo "urls:"
	@echo "${urls}"

clean:
	rm -fv *~ *.tmp tmp/*

install: ${THISDIR}/config/gbs.conf
	install -d ${DESTDIR}/usr/share/${package}/
	install $< ${DESTDIR}/usr/share/${package}/

deploy: ${THISDIR}/config/gbs.conf
	-mv -fv ${HOME}/.gbs.conf ${HOME}/.gbs.conf${tmp}
	ln -fs $< ${HOME}/.gbs.conf

rule/log/%: ${tmp_dir}
	mkdir -p ${tmp_dir}/${@D}
	script -e -c "${MAKE} rule/${@F}" ${tmp_dir}/$@

gbs/%: ${srcs}
	gbs ${@F} -P ${profile} --arch ${arch} --include-all

gbs-safe/%: 
	${MAKE} gbs/${@F} || ${MAKE} reset gbs/${@F}
	ls ${repo_dir}/${arch}/SRPMS/${package}*.src.rpm

rule/profile%_x86_64:
	${MAKE} ${gbs}/build profile=${@F} arch=x86_64

rule/profile%_i586:
	${MAKE} ${gbs}/build profile=${@F} arch=i586

rule/profile%_armv6l:
	${MAKE} ${gbs}/build profile=${@F} arch=armv6l

rule/profile%_armv7hl:
	${MAKE} ${gbs}/build profile=${@F} arch=armv7hl

rule/profile%_armv7l:
	${MAKE} ${gbs}/build profile=${@F} arch=armv7l

rule/profile%_aarch64:
	${MAKE} ${gbs}/build profile=${@F} arch=aarch64

rule/profile%_mips:
	${MAKE} ${gbs}/build profile=${@F} arch=mips

rule/profile%_mipsel:
	${MAKE} ${gbs}/build profile=${@F} arch=mipsel

tmp/%:
	mkdir -p ${@D}
	${MAKE} rule/${@F} > ${@} \
  && ln -fs "${@F}" "${@}.log._success.tmp" \
  && rm -f "${@}.log._fail.tmp" 

force/%:
	-mv -f tmp/${@F} tmp/${@F}.bak
	${MAKE} tmp/${@F}

check/%:
	-ls tmp/${@F}.log._success.tmp || ${MAKE} "force/${@F}" || echo "fail: ${@F}"
	-ls tmp/${@F}.log._success.tmp || ln -fs "${@F}" tmp/% "tmp/${@F}.log._fail.tmp" 
	-cat "tmp/${@F}.log._fail.tmp"
	ls tmp/${@F}.log._success.tmp

check: check/urls
	for profile in ${profiles} ; do \
  ${MAKE} check/$${profile} \
  || echo "fail: $${profile}"\
	;  done
	@echo "Failures ?"
	ls tmp/*.log._fail.tmp && exit 1 || echo "NONE: $@"

check/urls: tmp/urls-check
	@echo "Failures ?"
	cat $<._fail.tmp && exit 1 || echo "NONE: $@"

tmp/urls-check:
	mkdir -p ${@D}
	for url in ${urls} ; do \
 wget -O /dev/null $${url} >/dev/null 2>&1 \
 || echo "error: $@: $${url}" >> ${@}._fail.tmp ; done
	touch $@

#TODO: only change status?

rule/prep: umount

umount:
	[ ! -e ${mount_dir} ] || ${sudo} umount ${mount_dir}

reset: umount binfmt


binfmt: /proc/sys/fs/binfmt_misc/
	@echo "TODO: add tests"
	ls "$<" | while read file ; do \
  echo '-1'  | ${sudo} tee "$</$${file}" ; \
	done


${THISDIR}/config/gbs.conf: ${THISDIR}/config/gbs/ ${srcs}
	find $< -iname "*.conf.in" -exec cat '{}' \; | tee $@.tmp
	mv $@.tmp $@

all: ${THISDIR}/config/gbs.conf

.PHONY: default help


overide: /var/tmp/${USER}-gbs/tizen_common_armv7l.conf
	cat $< > $<.mine
	echo "" >>$<.mine
	echo "Prefer: libwayland-egl" >> $<.mine
